﻿using AutoUpdater.Bll;
using AutoUpdater.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace AutoUpdater
{
    public class UpdateFileUtil
    {
        private string basePath;
        private string tempPath;

        private FileContract _fileContract;

        private Thread thread;

        public UpdateFileUtil(FileContract fileContract)
        {
            tempPath = basePath + "temp";
            if(!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }
            _fileContract = fileContract;
        }

        public string downLoadFile()
        {
            if(_fileContract.deleteFiles != null && _fileContract.deleteFiles.Count>0)
            {
                foreach(var deleteFile in _fileContract.deleteFiles)
                {
                    File.Delete(Path.Combine(basePath, deleteFile.filePath, deleteFile.fileName));
                }
            }

            if (_fileContract.updateFiles != null && _fileContract.updateFiles.Count > 0)
            {
                foreach (var file in _fileContract.updateFiles)
                {
                    // 检查本地文件hash值是否一致，一致就不用下载了
                    int start = 0;
                    while (start < file.fileSize)
                    {
                        byte[] fileByte = FileCheckBll.DownLoadFile(file, 0);
                        // 写入文件
                    }

                    //下载完保存到本地
                }

                foreach (var file in _fileContract.updateFiles)
                {
                    File.Move(Path.Combine(tempPath, file.filePath, file.fileName), Path.Combine(basePath, file.filePath, file.fileName));
                }
            }

            return null;
        }
    }
}
